package xin.baizhiedu.config;

import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import xin.baizhiedu.Util.TimeUtils;
import xin.baizhiedu.entity.AppTaskFirst;
import xin.baizhiedu.entity.TBaseConfig;
import xin.baizhiedu.service.TaskFirstService;
import cn.hutool.http.HttpUtil;
import xin.baizhiedu.service.configDaoService;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class MyScheduledTasks {
    @Autowired
    TaskFirstService taskFirstService;

    @Autowired
    configDaoService configDao;

    private static int Value = 0;

    @Scheduled(cron = "0 0/5 * * * ?")// 每五分钟执行一次（300000毫秒）
    public void myTask1() {
        AppTaskFirst vo = new AppTaskFirst();
        vo.setProcessType( "2");
        List<AppTaskFirst> appTaskFirstList = taskFirstService.getTaskFirstList(vo);
        TBaseConfig robotId = configDao.findTBaseConfigByConfCode("QYWXQF");
        TBaseConfig UrlConfig = configDao.findTBaseConfigByConfCode("QYWX_YCGL");
        if(appTaskFirstList.size()>0){
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("未接收任务总数量：").append(appTaskFirstList.size());
            for (int i = 0; i < appTaskFirstList.size(); i++) {
                stringBuffer.append("\n").append("\n").append("现场标号：").append(appTaskFirstList.get(i).getOn_site_labeling() ==null ?"":appTaskFirstList.get(i).getOn_site_labeling());
                stringBuffer.append("\n").append(" 车间：").append(appTaskFirstList.get(i).getWorkshop() ==null ?"":appTaskFirstList.get(i).getWorkshop());
                stringBuffer.append("\n").append(" 设备：").append(appTaskFirstList.get(i).getMATO_NAME()==null ?"":appTaskFirstList.get(i).getMATO_NAME());
                stringBuffer.append("\n").append(" 报修人：").append(appTaskFirstList.get(i).getRepair_applicant());
                stringBuffer.append("\n").append(" 是否停机：").append(appTaskFirstList.get(i).getPROCESS_TYPE_NAME2()==null?"否":appTaskFirstList.get(i).getPROCESS_TYPE_NAME2().equals("设备停机")?"是":"否");
                stringBuffer.append("\n").append(" 发布时间：").append(appTaskFirstList.get(i).getRelease_time());
                stringBuffer.append("\n").append(" 故障类型：").append(appTaskFirstList.get(i).getPROCESS_TYPE_NAME1()==null ?"":appTaskFirstList.get(i).getPROCESS_TYPE_NAME1()).append(" ").append(appTaskFirstList.get(i).getPROCESS_TYPE_NAME2()==null ?"": appTaskFirstList.get(i).getPROCESS_TYPE_NAME2());
                stringBuffer.append("\n").append(" 故障描述：").append(appTaskFirstList.get(i).getFault_Description()==null ?"":appTaskFirstList.get(i).getFault_Description()).append("\n");
            }

            String nr= stringBuffer.toString();
            Map<String, Object> contexts = new HashMap<>();
            contexts.put("robotId",robotId.getConfValue());
            contexts.put("message",nr);
            contexts.put("textType","text");
            String qs = JSONUtil.toJsonStr(contexts);
            String qywx1 = HttpUtil.createPost(UrlConfig.getConfValue())
                    .body(qs)
                    .execute()
                    .body();
            System.out.println("五分钟："+qywx1);
        }
        System.out.println("五分钟："+appTaskFirstList.size());
    }

    @Scheduled(cron = "0 0/5 * * * ?")// 每五分钟执行一次（300000毫秒）
    public void myTask2() {
        AppTaskFirst vo = new AppTaskFirst();
        vo.setProcessType( "2");
        vo.setStates("1");
        List<AppTaskFirst> appTaskFirstList = taskFirstService.getTaskFirstList(vo);
        TBaseConfig robotId = configDao.findTBaseConfigByConfCode("QYWXQF");
        TBaseConfig UrlConfig = configDao.findTBaseConfigByConfCode("QYWX_YCGL");
        int a= 0;
        if(appTaskFirstList.size()>0){
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < appTaskFirstList.size(); i++) {
                if (TimeUtils.minutesDifference(new Date(), appTaskFirstList.get(i).getProcessing_time())) {
                    a=a+1;
                }
            }
            stringBuffer.append("接收任务未关闭总数量：").append(a);
            for (int i = 0; i < appTaskFirstList.size(); i++) {
                if(TimeUtils.minutesDifference(new Date(),appTaskFirstList.get(i).getProcessing_time())){
                    stringBuffer.append("\n").append("\n").append("现场标号：").append(appTaskFirstList.get(i).getOn_site_labeling() ==null ?"":appTaskFirstList.get(i).getOn_site_labeling());
                    stringBuffer.append("\n").append(" 车间：").append(appTaskFirstList.get(i).getWorkshop() ==null ?"":appTaskFirstList.get(i).getWorkshop());
                    stringBuffer.append("\n").append(" 设备：").append(appTaskFirstList.get(i).getMATO_NAME()==null ?"":appTaskFirstList.get(i).getMATO_NAME());
                    stringBuffer.append("\n").append(" 是否停机：").append(appTaskFirstList.get(i).getPROCESS_TYPE_NAME2()==null?"否":appTaskFirstList.get(i).getPROCESS_TYPE_NAME2().equals("设备停机")?"是":"否");
                    stringBuffer.append("\n").append(" 报修人：").append(appTaskFirstList.get(i).getRepair_applicant());
                    stringBuffer.append("\n").append(" 发布时间：").append(appTaskFirstList.get(i).getRelease_time());
                    stringBuffer.append("\n").append(" 接收人：").append(appTaskFirstList.get(i).getProcessed_by());
                    stringBuffer.append("\n").append(" 接收时间：").append(TimeUtils.Time(appTaskFirstList.get(i).getProcessing_time()));
                    stringBuffer.append("\n").append(" 故障类型：").append(appTaskFirstList.get(i).getPROCESS_TYPE_NAME1()==null ?"":appTaskFirstList.get(i).getPROCESS_TYPE_NAME1()).append(" ").append(appTaskFirstList.get(i).getPROCESS_TYPE_NAME2()==null ?"": appTaskFirstList.get(i).getPROCESS_TYPE_NAME2());
                    stringBuffer.append("\n").append(" 故障描述：").append(appTaskFirstList.get(i).getFault_Description()==null ?"":appTaskFirstList.get(i).getFault_Description()).append("\n");

                }
            }
            String nr= "@所有人 "+stringBuffer;
            Map<String, Object> contexts = new HashMap<>();
            contexts.put("robotId",robotId.getConfValue());
            contexts.put("message",nr);
            contexts.put("textType","text");
            String qs = JSONUtil.toJsonStr(contexts);
            System.out.println("两小时..."+Value);
            if(Value==0){
                Value = a;
            } else if (Value<a) {
                Value = a;
                String qywx1 = HttpUtil.createPost(UrlConfig.getConfValue())
                        .body(qs)
                        .execute()
                        .body();
                System.out.println("两小时..."+qywx1);
            }
        }
       // System.out.println("两小时..."+a);
    }

}